Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

310
Visualizações
What's the difference between np.array(int) and np.array([int])?

What's the difference between np.array(100) and np.array([100])? I understand that the latter is a 1D array containing a single value (100) but what is the former called?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This is a 0d array. It can be used in many of the same ways as other arrays, subject of course to shape and dtype compatibilites.

In [545]: x=np.array(3)
In [546]: x.shape
Out[546]: ()          # empty tuple
In [547]: x.ndim
Out[547]: 0
In [548]: x.ravel()
Out[548]: array([3])         # as with other arrays, ravel makes a 1d array
In [549]: x.reshape(1,1,1)    # reshape to 3d
Out[549]: array([[[3]]])
In [550]: x.item()            # extracting that element
Out[550]: 3
In [551]: x[()]               # another extracting
Out[551]: 3             
In [552]: type(_)
Out[552]: numpy.int64
In [553]: type(x.item())
Out[553]: int

There's a subtle difference between item() and [()]. One returns python object, the other a "numpy scalar".

More on numpy scalars:

https://numpy.org/doc/stable/reference/arrays.scalars.html#methods

A common case where we encounter 0d array is when an object gets wrapped in an array, such as via np.save.

In [556]: d = np.array({'foo':'bar'})
In [557]: d
Out[557]: array({'foo': 'bar'}, dtype=object)
In [558]: d.shape
Out[558]: ()
In [559]: d.item()['foo']
Out[559]: 'bar'

The value of a 0d array can be changed

In [562]: x[...] = 4
In [563]: x
Out[563]: array(4)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda